package hcn.mch.response;

import ctd.util.BeanUtils;
import ctd.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class AddressVo {
    private String province;
    private String provinceText;
    private String city;
    private String cityText;
    private String county;
    private String countyText;
    private String town;
    private String townText;
    private String village;
    private String villageText;
    private String houseNumber;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getProvinceText() {
        return provinceText;
    }

    public void setProvinceText(String provinceText) {
        this.provinceText = provinceText;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCityText() {
        return cityText;
    }

    public void setCityText(String cityText) {
        this.cityText = cityText;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getCountyText() {
        return countyText;
    }

    public void setCountyText(String countyText) {
        this.countyText = countyText;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getTownText() {
        return townText;
    }

    public void setTownText(String townText) {
        this.townText = townText;
    }

    public String getVillage() {
        return village;
    }

    public void setVillage(String village) {
        this.village = village;
    }

    public String getVillageText() {
        return villageText;
    }

    public void setVillageText(String villageText) {
        this.villageText = villageText;
    }

    public String getHouseNumber() {
        return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    public static Map<String,String> jsonToMap(String addressVoJsonStr, String keyPrefix){
        Map<String,String> rst = new HashMap<>();
        if (StringUtils.isEmpty(addressVoJsonStr)){
            return rst;
        }

        try {
            HashMap<String,String> parse = JSONUtils.parse(addressVoJsonStr, HashMap.class);
            for (Map.Entry<String, String> entry : parse.entrySet()) {
                String key = keyPrefix+firstLetterCapitalize(entry.getKey());
                String value = entry.getValue();
                rst.put(key,value);
            }
            return rst;
        }catch (Exception e){
            e.printStackTrace();
            return rst;
        }
    }

    public static String firstLetterCapitalize(String str) {
        byte[] items = str.getBytes();
        if (items[0]>='a' && items[0]<='z'){
            items[0] = (byte) ((char) items[0] - 'a' + 'A');
        }
        return new String(items);
    }

    public static String firstLetterLowercase(String str) {
        byte[] items = str.getBytes();
        if (items[0]>='A' && items[0]<='Z'){
            items[0] = (byte) ((char) items[0] - 'A' + 'a');
        }
        return new String(items);
    }

    public static void main(String[] args) {
        AddressVo addressVo = new AddressVo();
        addressVo.setProvinceText("aaaaaa");

        Map map = new HashMap();
        map.put("townText","bbbbbbb");

        BeanUtils.copy(map,addressVo);
        System.out.println("1");

        String json = "{\"province\":\"110000000000\",\"provinceText\":\"北京市\",\"city\":\"110100000000\",\"cityText\":\"市辖区\",\"county\":\"110101000000\",\"countyText\":\"东城区\",\"town\":\"110101001000\",\"townText\":\"东华门\",\"village\":\"110101001001\",\"villageText\":\"多福巷\",\"houseNumber\":\"213\"}";
        HashMap parse = JSONUtils.parse(json, HashMap.class);
        Map<String, String> husband = jsonToMap(json, "husband");
        System.out.println(parse);
    }
}
